import 'dart:io';

import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/common/common.dart';
import 'package:appflutterhomeschool/res/styles.dart';
import 'package:appflutterhomeschool/route/application.dart';
import 'package:appflutterhomeschool/route/routes.dart';
import 'package:appflutterhomeschool/ui/dialogs/prompt_dialog_widgets.dart';
import 'package:appflutterhomeschool/ui/widgets/my_app_bar.dart';
import 'package:appflutterhomeschool/ui/widgets/widgets.dart';
import 'package:appflutterhomeschool/view_model/my_app_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

///用户：Nine Tailed Fox
///时间：2020/6/11
///邮箱：1037438704@qq.com
///功能：身份选择页面
class SwitchIdentityPage extends StatefulWidget {
  final int pageTage;

  SwitchIdentityPage({this.pageTage});

  @override
  _SwitchIdentityPageState createState() => _SwitchIdentityPageState();
}

class _SwitchIdentityPageState extends State<SwitchIdentityPage> {
  TextEditingController textEditingController = TextEditingController();
  TextEditingController textEditingController2 = TextEditingController();
  MyAppViewModel myAppViewModel;


  @override
  Widget build(BuildContext context) {
    myAppViewModel = Provider.of<MyAppViewModel>(context);
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: MyAppBar2(
        title: "",
        elevation: 0,
        onTapLeft: () =>
            widget.pageTage == 0 ? exit(0) : Navigator.pop(context),
      ),
      body: Container(
        width: double.maxFinite,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Text("选择身份", style: TextStyles.textDark70),
            SizedBox(height: ScreenUtil().setWidth(43)),
            Text("选择身份登录，不一样的体验", style: TextStyles.textDark36),
            SizedBox(height: ScreenUtil().setWidth(91)),
            SwitchIdentityWidget(
              image: "jiazhang",
              visible: !myAppViewModel.identity,
              onTap: () {
                chaxunshenfen(context, "PARENT");
              },
            ),
            SwitchIdentityWidget(
              image: "jiaoshi",
              visible: myAppViewModel.identity,
              onTap: () {
                chaxunshenfen(context, "TEACHER");
              },
            ),
          ],
        ),
      ),
    );
  }

  //身份查询
  chaxunshenfen(BuildContext context, String shenfen) {
    //先查询是否有这个身份
    //查询身份
    DioUtils.getInstance().post(ApiService.roleExists, {
      "token": Constant.getToken(),
      "role": shenfen,
    }, (successCallBack) {
      //身份不存在  两种情况  1 家长  2 老师
      if (successCallBack["data"]["exists"] == "false") {
        PromptDialogWidgets(context).newIdentityDialog(
            textEditingController: textEditingController,
            textEditingController2: textEditingController2,
            identity: shenfen == "TEACHER",
            bottomName: "确定",
            onTap: () {
              Map<String, dynamic> mapdata = {};
              if (shenfen == "TEACHER") {
                mapdata = {
                  "token": Constant.getToken(),
                  "name": textEditingController.text,
                  "school": textEditingController2.text,
                };
              } else {
                mapdata = {
                  "token": Constant.getToken(),
                  "name": textEditingController.text,
                };
              }
              PromptDialogWidgets(context)
                  .showLoadingDialog((Function function) {
                DioUtils.getInstance().post(
                    shenfen == "TEACHER"
                        ? ApiService.createTeacher
                        : ApiService.createParent,
                    mapdata, (successCallBack) {
                  function();
                  myAppViewModel.globalSwitchOnTap(shenfen);
                  if (widget.pageTage == 0) {
                    Navigator.pop(context);
                    Application()
                        .navigatePushToPage(context, Routes.mainTabPage, {});
                  }
                }, (errorCallBack) {
                  function();
                });
              });
            });
      } else {
        myAppViewModel.globalSwitchOnTap(shenfen);
      }
    }, (errorCallBack) {});
  }
}
